unit Class_Dict;

interface
uses
  Classes,SysUtils,DateUtils,UniEngine;

type
  TFileDict=class(TUniEngine)
  private
    FOriginData:string;
    FFileDate:Integer;
    FFileName:string;
  public
    procedure ReadIt;
    function  GetObjtIdentity:string;
  published
    property FileName:string  read FFileName write FFileName;
    property FileDate:Integer read FFileDate write FFileDate;
  public
    class function  ListIt(AList:TStringList):TStringList;overload;//*list of *tfiledict
    class procedure ListIt(const DirSource:string;var AList:TStringList);overload;//*list;from local directory
  end;

implementation

uses
  Class_KzDebug,Class_KzUtils;

{ TFileDict }

function TFileDict.GetObjtIdentity: string;
begin
  Result:=Format('{filname:%S,filedate:%D}',[FFileName,FFileDate]);
  Result:=TOJSON();
end;

class function TFileDict.ListIt(AList: TStringList): TStringList;
var
  I:Integer;
  TMPA:string;
  Dict:TFileDict;
begin
  Result:=TStringList.Create;

  for I:=0 to AList.Count-1 do
  begin
    TMPA:=Trim(AList.Strings[I]);
    if TMPA='' then Continue;

    Dict:=TFileDict.Create;
    Dict.FOriginData:=TMPA;
    Dict.ReadIt;

    Result.AddObject(Dict.FileName,Dict);
  end;
end;

class procedure TFileDict.ListIt(const DirSource: string;
  var AList: TStringList);
var
  DirSearch:string;
  sr: TSearchRec;
  Attr: Integer;
  s: string;
begin
  DirSearch:=Format('%s\*.*',[DirSource]);
  Attr := faAnyFile;
  if FindFirst(DirSearch,Attr,sr)=0 then
  begin
    repeat
      if (sr.Name<>'.') and (sr.Name<>'..') then
      begin
        AList.Add(sr.Name);
      end;
    until(FindNext(sr)<>0);
  end;
  FindClose(sr);
end;

procedure TFileDict.ReadIt;
var
  YEAR:Integer;
  DAYA:string;
  MONTH:string;


  INDX:Integer;
  TMPA:string;
  List:TStrings;

  function getMonth(AValue:string):string;
  begin
    if AValue='JAN' then
    begin
      Result:='01';
    end else
    if AValue='FEB' then
    begin
      Result:='02';
    end else
    if AValue='MAR' then
    begin
      Result:='03'
    end else
    if AValue='APR' then
    begin
      Result:='04';
    end else
    if AValue='MAY' then 
    begin
      Result:='05';
    end else
    if AValue='JUN' then
    begin
      Result:='06';
    end else
    if AValue='JUL' then
    begin
      Result:='07';
    end else
    if AValue='AUG' then
    begin
      Result:='08';
    end else
    if AValue='SEP' then
    begin
      Result:='09';
    end else
    if AValue='OCT' then
    begin
      Result:='10';
    end else
    if AValue='NOV' then
    begin
      Result:='11';
    end else
    if AValue='DEC' then
    begin
      Result:='12';
    end;                    
  end;
begin
  try
    List:=TStringList.Create;
    List.DelimitedText:=FOriginData;
    

    FFileName:=Trim(List.Strings[8]);
    INDX:=Pos(FFileName,FOriginData);
    FFileName:=Copy(FOriginData,INDX,MaxInt);
    

    TMPA:=Trim(List.Strings[7]);
    if Pos(':',TMPA)>0 then
    begin
      YEAR:=YearOf(Now);
    end else
    begin
      YEAR:=StrToIntDef(TMPA,YearOf(Now));
    end;

    TMPA:=Trim(List.Strings[6]);
    DAYA:=TKzUtils.FormatCode(StrToInt(TMPA),2);

    TMPA:=UpperCase(Trim(List.Strings[5]));
    MONTH:=getMonth(TMPA);

    FFileDate:=StrToIntDef(Format('%D%S%S',[YEAR,MONTH,DAYA]),-1);
    KzDebug.FileLog(GetObjtIdentity);
  finally
    FreeAndNil(List);
  end;
end;

end.
